/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIUSer.java
 *
 * Created on 16-10-2011, 22:44:09
 */
package gui;

import Config.Localization;
import bl.BLCustomer;
import bl.BLUser;
import entities.Customer;
import entities.User;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HungVM
 */
public class GUICustomer extends javax.swing.JPanel {

    private ArrayList<Customer> customers = null;
    private Customer customer = null;
    private BLCustomer blCustomer = new BLCustomer();
    private User user = null;

    /** Creates new form GUIUSer */
    public GUICustomer(User user) {
        this.user = user;
        initComponents();
        customers = new ArrayList<Customer>();
        blCustomer = new BLCustomer();
        customer = new Customer();
        refreshData();
        canEdit(false);
        if (!user.getUserType().getTypeName().equals("Employee")) {
            //btnCreateNew.setEnabled(false);
            btnNewLoan.setEnabled(false);
        }
        System.out.print("User Type ID = " + user.getUserType().getTypeId());
        if (user.getUserType().getTypeId() == 1 || user.getUserType().getTypeId() == 2) {
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            btnCreateNew.setEnabled(false);
            btnAdd.setEnabled(false);
            btnNewLoan.setEnabled(false);
        }

    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgGender = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblCustomerID = new javax.swing.JLabel();
        txtFirstName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtBirthday = new com.toedter.calendar.JDateChooser();
        txtIdentity = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        rdoMale = new javax.swing.JRadioButton();
        rdoFemale = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taNote = new javax.swing.JTextArea();
        txtOrganzination = new javax.swing.JTextField();
        txtGrossSalary = new javax.swing.JTextField();
        btnNewLoan = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        txtSearch = new javax.swing.JTextField();
        cboSelect = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        btnList = new javax.swing.JButton();
        btnCreateNew = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblList = new javax.swing.JTable();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("CustomerID");

        jLabel2.setText("First Name");

        jLabel3.setText("Last Name");

        jLabel4.setText("Address");

        jLabel5.setText("BirthDay");

        jLabel6.setText("Email");

        jLabel7.setText("Identity");

        jLabel8.setText("Gender");

        jLabel9.setText("Gross Salary");

        jLabel10.setText("Organzination");

        jLabel11.setText("Phone");

        txtBirthday.setDateFormatString("MM/dd/yyyy");

        jLabel13.setText("Note");

        btgGender.add(rdoMale);
        rdoMale.setText("Male");

        btgGender.add(rdoFemale);
        rdoFemale.setText("Female");

        taNote.setColumns(20);
        taNote.setRows(5);
        jScrollPane1.setViewportView(taNote);

        txtGrossSalary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGrossSalaryActionPerformed(evt);
            }
        });

        btnNewLoan.setText("New Loan");
        btnNewLoan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewLoanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtIdentity)
                        .addComponent(txtEmail)
                        .addComponent(txtLastName)
                        .addComponent(txtAddress)
                        .addComponent(txtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                        .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(77, 77, 77)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(4, 4, 4))
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(5, 5, 5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtGrossSalary, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                            .addComponent(txtOrganzination, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rdoMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rdoFemale))
                            .addComponent(txtPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                        .addGap(107, 107, 107))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnNewLoan)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(rdoMale)
                            .addComponent(rdoFemale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtOrganzination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel6))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(txtIdentity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(txtGrossSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNewLoan)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtSearch.setColumns(16);
        txtSearch.setText("Enter to search");
        txtSearch.setPreferredSize(new java.awt.Dimension(134, 25));
        txtSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                txtSearchMousePressed(evt);
            }
        });
        jPanel3.add(txtSearch);

        cboSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "By ID", "By Name", "By Organzination", "By Identity", "By Gross Salary" }));
        cboSelect.setMinimumSize(new java.awt.Dimension(51, 25));
        cboSelect.setPreferredSize(new java.awt.Dimension(70, 25));
        jPanel3.add(cboSelect);

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/find.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        jPanel3.add(btnSearch);

        btnList.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/list1.png"))); // NOI18N
        btnList.setText("List");
        btnList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListActionPerformed(evt);
            }
        });
        jPanel3.add(btnList);

        btnCreateNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/new2.png"))); // NOI18N
        btnCreateNew.setText("Create New");
        btnCreateNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateNewActionPerformed(evt);
            }
        });
        jPanel3.add(btnCreateNew);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/add_small.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel3.add(btnAdd);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Text-Edit-icon.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jPanel3.add(btnEdit);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/DeleteRed.png"))); // NOI18N
        btnDelete.setText("Disable");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel3.add(btnDelete);

        tblList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblListMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tblList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 829, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 819, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void txtGrossSalaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGrossSalaryActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtGrossSalaryActionPerformed

private void btnListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListActionPerformed
// TODO add your handling code here:
    refreshData();
}//GEN-LAST:event_btnListActionPerformed

private void tblListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListMouseReleased
// TODO add your handling code here:
    int row = tblList.getSelectedRow();
    customer = customers.get(row);
    if (row >= customers.size()) {
        return;
    }
    lblCustomerID.setText(String.valueOf(customers.get(row).getCustomeId()));
    txtFirstName.setText(customers.get(row).getFirstName());
    txtLastName.setText(customers.get(row).getLastName());
    txtEmail.setText(customers.get(row).getEmail());
    txtAddress.setText(customers.get(row).getAddress());
    txtBirthday.setDate(customers.get(row).getBirthDay());
    txtGrossSalary.setText(String.valueOf(customers.get(row).getGrossSalary()));
    txtIdentity.setText(customers.get(row).getIdentity());
    txtOrganzination.setText(customers.get(row).getOrganzination());
    txtPhone.setText(customers.get(row).getPhone());
    rdoMale.setSelected(customers.get(row).isSex());
    rdoFemale.setSelected(!customers.get(row).isSex());
    taNote.setText(customers.get(row).getNote());

    btnAdd.setEnabled(false);
    btnEdit.setEnabled(true);
    btnDelete.setEnabled(true);
    if (customers.get(row).isStatus()) {
        btnNewLoan.setEnabled(false);
    } else {
        btnNewLoan.setEnabled(true);
    }
    canEdit(true);
    if (customers.get(row).isStatus()) {
        btnDelete.setText("Enable");
    } else {
        btnDelete.setText("Disable");
    }

    if (user.getUserType().getTypeId() == 1 || user.getUserType().getTypeId() == 2) {
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            btnCreateNew.setEnabled(false);
            btnAdd.setEnabled(false);
            btnNewLoan.setEnabled(false);
        }

}//GEN-LAST:event_tblListMouseReleased

private void btnCreateNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateNewActionPerformed
// TODO add your handling code here:
    reset();
    btnAdd.setEnabled(true);
    btnEdit.setEnabled(false);
    btnDelete.setEnabled(false);
    lblCustomerID.setText("");
    canEdit(true);
    txtFirstName.setFocusable(true);
}//GEN-LAST:event_btnCreateNewActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
    try {
        int n = Integer.parseInt(lblCustomerID.getText());
        int choice = JOptionPane.showConfirmDialog(null, Localization.getMessage("Msg15"), "Confirm", JOptionPane.OK_CANCEL_OPTION);
        if (choice == 0) {
            if (customer.isStatus()) {
                if (blCustomer.ennableCustomer(n)) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Success"), "Information", JOptionPane.INFORMATION_MESSAGE);
                    refreshData();
                } else {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Information", JOptionPane.INFORMATION_MESSAGE);
                }
            } else {
                if (blCustomer.deleteCustomer(n)) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Success"), "Information", JOptionPane.INFORMATION_MESSAGE);
                    refreshData();
                } else {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Information", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Notice", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
    Customer cus = initCustomer();


    if (cus != null) {
        cus.setCustomeId(Integer.parseInt(lblCustomerID.getText()));
        cus.setUser(user);
        try {
            if (blCustomer.updateCustomer(cus)) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Success"), "Infomation", JOptionPane.INFORMATION_MESSAGE);
                this.refreshData();
            } else {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Fail"), "Infomation", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}//GEN-LAST:event_btnEditActionPerformed

private void txtSearchMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtSearchMousePressed
// TODO add your handling code here:
    txtSearch.setText("");
}//GEN-LAST:event_txtSearchMousePressed

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
    Customer cus = initCustomer();
    try {
        if (cus != null) {
            cus.setUser(user);
            if (blCustomer.checkIdentity(txtIdentity.getText().trim())) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg14"));
                return;
            }
            if (blCustomer.insertCustomer(cus, user.getUserId())) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Success"));
                this.refreshData();
            } else {
                if (BLUser.f == 1) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg8"));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }

}//GEN-LAST:event_btnAddActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
    int index = cboSelect.getSelectedIndex();
    switch (index) {
        case 0: //search by Id
            try {
                int id = Integer.parseInt(txtSearch.getText());
                customers = blCustomer.getByID(id, user);
                if (customers.isEmpty()) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                }
                loadModel(customers);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg13"));
            }
            break;
        case 1: //search by username

            customers = blCustomer.getByName(txtSearch.getText(), user);
            if (customers.isEmpty()) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
            }
            loadModel(customers);
            break;

        case 2:
            customers = blCustomer.getByOrganzination(txtSearch.getText(), user);
            if (customers.isEmpty()) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
            }
            loadModel(customers);
            break;
        case 3:
            try {
                int id = Integer.parseInt(txtSearch.getText());
                customers = blCustomer.getByIdentity(txtSearch.getText(), user);
                if (customers.isEmpty()) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                }
                loadModel(customers);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg13"));
            }
            break;
        case 4:
            try {
                double dGross = Double.parseDouble(txtSearch.getText());
                customers = blCustomer.getByGross(dGross, user);
                if (customers.isEmpty()) {
                    JOptionPane.showMessageDialog(null, Localization.getMessage("Msg12"));
                }
                loadModel(customers);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("Msg13"));
            }
            break;
    }
}//GEN-LAST:event_btnSearchActionPerformed

private void btnNewLoanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewLoanActionPerformed
// TODO add your handling code here:
    GUINewLoan newLoan = new GUINewLoan(null, true, user, customer);
    newLoan.setVisible(true);

}//GEN-LAST:event_btnNewLoanActionPerformed

    public void reset() {
        lblCustomerID.setText("");
        txtAddress.setText("");
        txtEmail.setText("");
        txtFirstName.setText("");
        txtIdentity.setText("");
        txtLastName.setText("");
        txtEmail.setText("");
        txtPhone.setText("");
        txtOrganzination.setText("");
        txtGrossSalary.setText("");
        taNote.setText("");
        txtBirthday.setDate(null);
        rdoFemale.setSelected(false);
        rdoMale.setSelected(false);
        btnAdd.setEnabled(false);
        btnCreateNew.setEnabled(true);
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        txtAddress.setEditable(false);
        txtBirthday.setEnabled(false);
        txtEmail.setEditable(false);
        txtFirstName.setEditable(false);
        txtLastName.setEditable(false);
        txtGrossSalary.setEditable(false);
        txtOrganzination.setEditable(false);
        txtPhone.setEditable(false);
        txtIdentity.setEditable(false);
    }

    public Customer initCustomer() {
        Customer cus = new Customer();

        if (!validateCustomer()) {
            return null;
        }
        if (!blCustomer.validateEmail(txtEmail.getText())) {
            return null;
        }
        cus.setAddress(txtAddress.getText());
        if (txtBirthday.getDate() != null) {
            java.sql.Date date = new java.sql.Date(txtBirthday.getDate().getTime());
            cus.setBirthDay(date);
        }

        cus.setEmail(txtEmail.getText());
        cus.setFirstName(txtFirstName.getText());
        cus.setLastName(txtLastName.getText());
        cus.setIdentity(txtIdentity.getText().trim());
        cus.setNote(taNote.getText());
        cus.setPhone(txtPhone.getText());
        cus.setSex(rdoMale.isSelected());
        cus.setGrossSalary(Double.parseDouble(txtGrossSalary.getText()));
        cus.setOrganzination(txtOrganzination.getText());
        cus.setAddress(txtAddress.getText());
        return cus;
    }

    public void refreshData() {
        try {
            this.customers = blCustomer.getAll(user);
        } catch (Exception e) {
        }
        loadModel(customers);
        reset();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnAdd.setEnabled(false);
        btnNewLoan.setEnabled(false);
    }

    public void loadModel(ArrayList<Customer> customers) {
        DefaultTableModel mode = new DefaultTableModel();
        mode.addColumn("ID");
        mode.addColumn("Fullname");
        mode.addColumn("Birthday");
        mode.addColumn("Phone number");
        mode.addColumn("Email");
        mode.addColumn("Identity");
        mode.addColumn("Status");

        mode.setRowCount(customers.size());
        int i = 0;
        for (Customer n : customers) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            mode.setValueAt(n.getCustomeId(), i, 0);
            mode.setValueAt(n.getFullname(), i, 1);
            mode.setValueAt(sdf.format(n.getBirthDay()), i, 2);
            mode.setValueAt(n.getPhone(), i, 3);
            mode.setValueAt(n.getEmail(), i, 4);
            mode.setValueAt(n.getIdentity(), i, 5);
            if (!n.isStatus()) {
                mode.setValueAt("Available", i, 6);
            } else {
                mode.setValueAt("Not available", i, 6);
            }
            i++;
        }
        this.tblList.setModel(mode);
    }

    public boolean validateCustomer() {
        List<JTextField> txts = new ArrayList<JTextField>();
        txts.add(txtIdentity);
        txts.add(txtFirstName);
        txts.add(txtLastName);
        txts.add(txtEmail);
        txts.add(txtPhone);
        txts.add(txtOrganzination);
        txts.add(txtGrossSalary);
        boolean check = true;
        for (int i = 0; i < txts.size(); i++) {
            if (txts.get(i).getText().length() == 0) {
                check = false;
            }
        }
        if (!check) {
            JOptionPane.showMessageDialog(this, Localization.getMessage("Msg15"));
        }
        return check;
    }

    public void canEdit(boolean isEdit) {
        txtAddress.setEditable(isEdit);
        txtBirthday.setEnabled(isEdit);
        txtEmail.setEditable(isEdit);
        txtFirstName.setEditable(isEdit);
        txtGrossSalary.setEditable(isEdit);
        txtIdentity.setEditable(isEdit);
        txtLastName.setEditable(isEdit);
        txtOrganzination.setEditable(isEdit);
        txtPhone.setEditable(isEdit);
        taNote.setEditable(isEdit);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgGender;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCreateNew;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnList;
    private javax.swing.JButton btnNewLoan;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCustomerID;
    private javax.swing.JRadioButton rdoFemale;
    private javax.swing.JRadioButton rdoMale;
    private javax.swing.JTextArea taNote;
    private javax.swing.JTable tblList;
    private javax.swing.JTextField txtAddress;
    private com.toedter.calendar.JDateChooser txtBirthday;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtGrossSalary;
    private javax.swing.JTextField txtIdentity;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtOrganzination;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
